/**
 * 
 */
package com.iwords.web.action;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iwords.service.object.ExamSentencesEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.iwords.service.LearningStatusServiceLocal;
import com.iwords.service.UserServiceLocal;
import com.iwords.service.WordServiceLocal;  
import com.iwords.utils.CodeUtils;
import com.iwords.constants.Constants;
import com.iwords.service.IWordsSearchServiceLocal;
import com.iwords.utils.StringUtils;
import com.iwords.service.object.SysLibWordsEntity;
import com.iwords.web.base.BaseAction;

/**
 * @author      BOLIN.HBC
 * @time        2011-3-19
 * @description 单词的SERVLET
 */
@WebServlet(name = "wordAction", urlPatterns = { "/word" })
public class WordAction extends BaseAction<SysLibWordsEntity> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	WordServiceLocal wordService;
    @EJB
    IWordsSearchServiceLocal iWordsSearchService;
    @EJB
    UserServiceLocal userService; 
    
    @EJB
    LearningStatusServiceLocal learningStatusService;
    

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		String type = req.getParameter("action");
		if (Constants.WORDS_LIST_ACTION.equals(type)) {
			doFindWordsBySysLibId(req, res);
		} else if (Constants.WORDS_SINGLE_ACTION.equals(type)) {
			doFindWordByWordId(req, res);
		} else if ("ee".equals(type)) {
			doSearchSentenceByWordID(req, res);
		}

	}

	public void doSearchSentenceByWordID(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {
		Long wordid = null;
		if (StringUtils.isNotBlank(req.getParameter("wordid"))) {
			wordid = Long.parseLong(req.getParameter("wordid"));
		}
		JSONObject result = new JSONObject();
        if(wordid == null){
            result.put(Constants.IS_SUCCESS,Boolean.FALSE);
            result.put("msg","参数为空啊");

        }else{
            List<ExamSentencesEntity> sentenceList =    iWordsSearchService.searchSentences(wordid, 0, 100);
            JSONArray defaultValue = new JSONArray();
            for (ExamSentencesEntity sentence : sentenceList) {

			JSONObject value = new JSONObject();

			value.put("sid", sentence.getSentenceid());
			value.put("sen", sentence.getSentence());
			value.put("eid", sentence.getExamid());
			value.put("iid", sentence.getItemid());

			defaultValue.put(value);
		}
        }

    }


	public void doFindWordsBySysLibId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		SysLibWordsEntity entity = initEntity(req);

		List<SysLibWordsEntity> resultList = wordService.findWordsBySysLibId(entity.getLibid());

		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		JSONArray defaultValue = new JSONArray();
		for (SysLibWordsEntity word : resultList) {

			JSONObject value = new JSONObject();

			value.put("wordid", word.getWordid());
			value.put("syslibid", word.getLibid());
			value.put("meaning", CodeUtils.encode(word.getMeaning()));
			value.put("phonetics", CodeUtils.encode(word.getPhonetics()));
			value.put("spelling", CodeUtils.encode(word.getSpelling()));
			value.put("createtime", word.getCreatetime());
			value.put("updatetime", word.getUpdatetime());
			defaultValue.put(value);
		}
		result.put(Constants.DEFAULT_VALUE, defaultValue);


	}
	public void doFindWordByWordId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		SysLibWordsEntity entity = initEntity(req);

		SysLibWordsEntity resultEntity = wordService.findWordByWordId(entity.getWordid());
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		if (resultEntity != null) {

			JSONObject defaultValue = new JSONObject();

			defaultValue.put("wordid", resultEntity.getWordid());
			defaultValue.put("syslibid", resultEntity.getLibid());
			defaultValue.put("meaning", CodeUtils.encode(resultEntity.getMeaning()));
			defaultValue.put("phonetics", resultEntity.getPhonetics());
			defaultValue.put("spelling", resultEntity.getSpelling());
			defaultValue.put("createtime", resultEntity.getCreatetime());
			defaultValue.put("updatetime", resultEntity.getUpdatetime());
			result.put("defaultValue", defaultValue);
		} else {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, CodeUtils.encode("该单词不存在"));

		}


	}

	public SysLibWordsEntity initEntity(HttpServletRequest req) {

		SysLibWordsEntity entity = new SysLibWordsEntity();
		String wordid = req.getParameter("wordid");
		if (StringUtils.isNotBlank(wordid)) {
			entity.setWordid(Long.parseLong(req.getParameter("wordid")));
		}
		String libid = req.getParameter("libid");
		if (StringUtils.isNotBlank(libid)) {
			entity.setLibid(Long.parseLong(req.getParameter("libid")));
		}

		return entity;
	}

}
